package com.project.petLossSystem.controller.admin;

import com.project.petLossSystem.service.ContactItemService;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.Result;
import com.project.petLossSystem.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ContactItemController {
    @Autowired
    private ContactItemService contactItemService;

    @GetMapping("/contactItems")
    public String petPost() {
        return "/pc/admin/contact_items";
    }

    @GetMapping("/contactItems/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("page")) || ObjectUtils.isEmpty(params.get("rows"))) {
            return ResultGenerator.failureResult("参数异常！");
        }
        // 判断userId是否有效
        if (!ObjectUtils.isEmpty(params.get("userId"))) {
            Long userId = Long.valueOf((String) params.get("userId"));
            params.put("userId", userId);
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResultGenerator.successResult(contactItemService.getItemPage(pageQueryUtil));
    }
}
